package pri.jz.keeper.jui.jdialog

import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.AlertDialog
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.DialogProperties
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.lifecycle.viewmodel.compose.viewModel
import pri.jz.keeper.R
import pri.jz.keeper.ui.base.BaseViewModel
import pri.jz.keeper.ui.theme.Shapes
import pri.jz.keeper.ui.view.MainButton

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:常用对话框
 */

/**
 * 提示对话框
 * @param state 对话框状态
 * @param tip 提示文字
 * @param btnText 按钮文字
 * @param dismissOnClickOutside 是否可点击外界取消
 * @param onClick 按钮点击事件
 */
@Composable fun JTipDialog(
    dismissOnClickOutside: Boolean = false,
    viewModel: BaseViewModel = viewModel(),
) {
    val tipDialogModel by viewModel.tipDialogUiState.collectAsState()

    if (tipDialogModel.state) {
        AlertDialog(
            modifier = Modifier.size(300.dp,200.dp),
            onDismissRequest = { viewModel.disTip() },
            title = {},
            buttons = {
                Box(
                    modifier = Modifier.fillMaxWidth(),
                    contentAlignment = Alignment.Center
                ) {
                    MainButton(text = tipDialogModel.btnText, modifier = Modifier.padding(vertical = 20.dp)) {
                        tipDialogModel.btnClick()
                    }
                }
            },
            text = {
                Box(
                    modifier = Modifier
                        .fillMaxWidth(),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = tipDialogModel.text, fontSize = 18.sp, color = Color.Black)
                }
            },
            properties = DialogProperties(dismissOnClickOutside = dismissOnClickOutside),
            shape = Shapes.medium
        )
    }
}

/**
 * 无进度等待对话框
 * @param state 对话框状态
 * @param dismissOnClickOutside 是否可点击外界取消
 * @param onDismissRequest 取消事件
 */
@Composable
fun JLoadingDialog(
    viewModel: BaseViewModel = viewModel(),
    dismissOnClickOutside: Boolean = false,
    onDismissRequest: () -> Unit = {viewModel.disLoading()}
) {
    val state by viewModel.loadDialogUiState.collectAsState()
    if (state) {
        AlertDialog(
            onDismissRequest = onDismissRequest,
            buttons = {},
            text = {
                JLoadingDialogView()
            },
            modifier = Modifier.size(300.dp,200.dp),
            backgroundColor = Color.Transparent,
            properties = DialogProperties(dismissOnClickOutside = dismissOnClickOutside)
        )
    }
}
@Composable
private fun JLoadingDialogView() {
    ConstraintLayout(
        modifier = Modifier.fillMaxSize().background(Color.Transparent)
    ) {
        val (imgLoad) = createRefs()
        val state = remember { mutableStateOf(true) }
        val rotationAnimate by animateFloatAsState(
            targetValue = if (!state.value) -180f else 0f,
            animationSpec = infiniteRepeatable(
                repeatMode = RepeatMode.Restart,
                animation = tween(
                    durationMillis = 1500
                )
            )
        )
        Image(
            painter = painterResource(R.drawable.img_loading),
            contentDescription = null,
            modifier = Modifier.constrainAs(imgLoad) {
                centerVerticallyTo(parent)
                centerHorizontallyTo(parent)
            }.graphicsLayer(rotationZ = rotationAnimate)
        )
        state.value = false
    }
}